package wai.school.method;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import wai.school.R;

/**
 * Created by Finder丶畅畅 on 2017/3/12 23:11
 * QQ群481606175
 */

public class NumberedAdapter extends RecyclerView.Adapter<TextViewHolder> {

    TextViewHolder.ItemClickListener listener;
    private List<String> labels;

    public NumberedAdapter(int count) {
        labels = new ArrayList<String>(count);
        for (int i = 0; i < count; ++i) {
            labels.add(String.valueOf(i));
        }
    }

    /**
     * 设置Item点击监听
     *
     * @param listener
     */
    public void setOnItemClickListener(TextViewHolder.ItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public TextViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main, parent, false);
        return new TextViewHolder(view, listener);
    }

    @Override
    public void onBindViewHolder(final TextViewHolder holder, final int position) {
        final String label = labels.get(position);
        holder.textView.setText(label);
        holder.textView.setOnClickListener(v -> {

        });
    }

    @Override
    public int getItemCount() {
        return labels.size();
    }

}

class TextViewHolder extends RecyclerView.ViewHolder {
    public TextView textView;
    public LinearLayout tag_ll;
    private ItemClickListener listener;

    public TextViewHolder(View itemView, ItemClickListener clickListener) {
        super(itemView);
        listener = clickListener;
        textView = (TextView) itemView.findViewById(R.id.text);
        tag_ll = (LinearLayout) itemView.findViewById(R.id.tag_ll);
        tag_ll.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(v, getAdapterPosition());
            }
        });
    }

    public interface ItemClickListener {
        void onItemClick(View view, int postion);
    }
}
